import QtQuick 2.0

Item {
    id:recipeParent;
    width: 1670; height: 820

    property string selected : "#FF274E13"
    property string unselected :  "#FF38761D"
    property string dish

    function startUp(){
        recipeFlickable.visible = false;
        recipeSelector.visible = true;
        dish = " ";
    }

    function loadRecipes(_dish) {
        dish = _dish;
        recipeSelector.visible = false;

        var doc = new XMLHttpRequest();
        doc.onreadystatechange = function() {
           if (doc.readyState == XMLHttpRequest.DONE) {
               var jsonObject = eval('(' + doc.responseText + ')');
               loadJSON(jsonObject,parseInt(1));
           }
        }
        // Replace YOURPRIVATEKEY by your key from free.worldweatheronline.com
        doc.open("GET", "http://food2fork.com/F2F/api/search?key=c4c777e50d6e067663bd76e28326fbbc&q="+dish);
        doc.send();

    }

    function loadJSON(jsonObject,offset)
    {
        recipeItemOne.titleOne = jsonObject.recipes[0].title;
        recipeItemOne.imageUrlOne = jsonObject.recipes[0].image_url;
        recipeItemOne.sourceUrlOne = jsonObject.recipes[0].source_url;
        ratingCalculator(recipeItemOne,parseInt(jsonObject.recipes[0].social_rank),1);
        //console.log(jsonObject.recipes[0].source_url);
       // recipeItemOne.socialRankOne = parseInt(jsonObject.recipes[0].social_rank);
        recipeItemOne.titleTwo = jsonObject.recipes[1].title;
        recipeItemOne.imageUrlTwo = jsonObject.recipes[1].image_url;
        recipeItemOne.sourceUrlTwo = jsonObject.recipes[1].source_url;
        ratingCalculator(recipeItemOne,parseInt(jsonObject.recipes[1].social_rank),2);
       // recipeItemOne.socialRankTwo = parseInt(jsonObject.recipes[1].social_rank);
        recipeItemOne.visible = true;
        recipeItemTwo.titleOne = jsonObject.recipes[2].title;
        recipeItemTwo.imageUrlOne = jsonObject.recipes[2].image_url;
        recipeItemTwo.sourceUrlOne = jsonObject.recipes[2].source_url;
        ratingCalculator(recipeItemTwo,parseInt(jsonObject.recipes[2].social_rank),1);
       // recipeItemTwo.socialRankOne = parseInt(jsonObject.recipes[2].social_rank);
        recipeItemTwo.titleTwo = jsonObject.recipes[3].title;
        recipeItemTwo.imageUrlTwo = jsonObject.recipes[3].image_url;
        recipeItemTwo.sourceUrlTwo = jsonObject.recipes[3].source_url;
        ratingCalculator(recipeItemTwo,parseInt(jsonObject.recipes[3].social_rank),2);
       // recipeItemTwo.socialRankTwo = parseInt(jsonObject.recipes[3].social_rank);
        recipeItemTwo.visible = true;
        recipeItemThree.titleOne = jsonObject.recipes[4].title;
        recipeItemThree.imageUrlOne = jsonObject.recipes[4].image_url;
        recipeItemThree.sourceUrlOne = jsonObject.recipes[4].source_url;
        ratingCalculator(recipeItemThree,parseInt(jsonObject.recipes[4].social_rank),1);
       // recipeItemThree.socialRankOne = parseInt(jsonObject.recipes[4].social_rank);
        recipeItemThree.titleTwo = jsonObject.recipes[5].title;
        recipeItemThree.imageUrlTwo = jsonObject.recipes[5].image_url;
        recipeItemThree.sourceUrlTwo = jsonObject.recipes[5].source_url;
        ratingCalculator(recipeItemThree,parseInt(jsonObject.recipes[5].social_rank),2);
       // recipeItemThree.socialRankTwo = parseInt(jsonObject.recipes[5].social_rank);
        recipeItemThree.visible = true;
        recipeItemFour.titleOne = jsonObject.recipes[6].title;
        recipeItemFour.imageUrlOne = jsonObject.recipes[6].image_url;
        recipeItemFour.sourceUrlOne = jsonObject.recipes[6].source_url;
        ratingCalculator(recipeItemFour,parseInt(jsonObject.recipes[6].social_rank),1);
       // recipeItemFour.socialRankOne = parseInt(jsonObject.recipes[6].social_rank);
        recipeItemFour.titleTwo = jsonObject.recipes[7].title;
        recipeItemFour.imageUrlTwo = jsonObject.recipes[7].image_url;
        recipeItemFour.sourceUrlTwo = jsonObject.recipes[7].source_url;
        ratingCalculator(recipeItemFour,parseInt(jsonObject.recipes[7].social_rank),2);
       // recipeItemFour.socialRankTwo = parseInt(jsonObject.recipes[7].social_rank);
        recipeItemFour.visible = true;
        recipeItemFive.titleOne = jsonObject.recipes[8].title;
        recipeItemFive.imageUrlOne = jsonObject.recipes[8].image_url;
        recipeItemFive.sourceUrlOne = jsonObject.recipes[8].source_url;
        ratingCalculator(recipeItemFive,parseInt(jsonObject.recipes[8].social_rank),1);
       // recipeItemFive.socialRankOne = parseInt(jsonObject.recipes[8].social_rank);
        recipeItemFive.titleTwo = jsonObject.recipes[9].title;
        recipeItemFive.imageUrlTwo = jsonObject.recipes[9].image_url;
        recipeItemFive.sourceUrlTwo = jsonObject.recipes[9].source_url;
        ratingCalculator(recipeItemFive,parseInt(jsonObject.recipes[9].social_rank),2);
       // recipeItemFive.socialRankTwo = parseInt(jsonObject.recipes[9].social_rank);
        recipeItemFive.visible = true;
        recipeItemSix.titleOne = jsonObject.recipes[10].title;
        recipeItemSix.imageUrlOne = jsonObject.recipes[10].image_url;
        recipeItemSix.sourceUrlOne = jsonObject.recipes[10].source_url;
        ratingCalculator(recipeItemSix,parseInt(jsonObject.recipes[10].social_rank),1);
       // recipeItemSix.socialRankOne = parseInt(jsonObject.recipes[10].social_rank);
        recipeItemSix.titleTwo = jsonObject.recipes[11].title;
        recipeItemSix.imageUrlTwo = jsonObject.recipes[11].image_url;
        recipeItemSix.sourceUrlTwo = jsonObject.recipes[11].source_url;
        ratingCalculator(recipeItemSix,parseInt(jsonObject.recipes[11].social_rank),2);
       // recipeItemSix.socialRankTwo = parseInt(jsonObject.recipes[11].social_rank);
        recipeItemSix.visible = true;
        recipeItemSeven.titleOne = jsonObject.recipes[12].title;
        recipeItemSeven.imageUrlOne = jsonObject.recipes[12].image_url;
        recipeItemSeven.sourceUrlOne = jsonObject.recipes[12].source_url;
        ratingCalculator(recipeItemSeven,parseInt(jsonObject.recipes[12].social_rank),1);
       // recipeItemSeven.socialRankOne = parseInt(jsonObject.recipes[12].social_rank);
        recipeItemSeven.titleTwo = jsonObject.recipes[13].title;
        recipeItemSeven.imageUrlTwo = jsonObject.recipes[13].image_url;
        recipeItemSeven.sourceUrlTwo = jsonObject.recipes[13].source_url;
        ratingCalculator(recipeItemSeven,parseInt(jsonObject.recipes[13].social_rank),2);
       // recipeItemSeven.socialRankTwo = parseInt(jsonObject.recipes[13].social_rank);
        recipeItemSeven.visible = true;
        recipeItemEight.titleOne = jsonObject.recipes[14].title;
        recipeItemEight.imageUrlOne = jsonObject.recipes[14].image_url;
        recipeItemEight.sourceUrlOne = jsonObject.recipes[14].source_url;
        ratingCalculator(recipeItemEight,parseInt(jsonObject.recipes[14].social_rank),1);
       // recipeItemEight.socialRankOne = parseInt(jsonObject.recipes[14].social_rank);
        recipeItemEight.titleTwo = jsonObject.recipes[15].title;
        recipeItemEight.imageUrlTwo = jsonObject.recipes[15].image_url;
        recipeItemEight.sourceUrlTwo = jsonObject.recipes[15].source_url;
        ratingCalculator(recipeItemEight,parseInt(jsonObject.recipes[15].social_rank),2);
       // recipeItemEight.socialRankTwo = parseInt(jsonObject.recipes[15].social_rank);
        recipeItemEight.visible = true;
        recipeItemNine.titleOne = jsonObject.recipes[16].title;
        recipeItemNine.imageUrlOne = jsonObject.recipes[16].image_url;
        recipeItemNine.sourceUrlOne = jsonObject.recipes[16].source_url;
        ratingCalculator(recipeItemNine,parseInt(jsonObject.recipes[16].social_rank),1);
       // recipeItemNine.socialRankOne = parseInt(jsonObject.recipes[16].social_rank);
        recipeItemNine.titleTwo = jsonObject.recipes[17].title;
        recipeItemNine.imageUrlTwo = jsonObject.recipes[17].image_url;
        recipeItemNine.sourceUrlTwo = jsonObject.recipes[17].source_url;
        ratingCalculator(recipeItemNine,parseInt(jsonObject.recipes[17].social_rank),2);
       // recipeItemNine.socialRankTwo = parseInt(jsonObject.recipes[17].social_rank);
        recipeItemNine.visible = true;
        recipeItemTen.titleOne = jsonObject.recipes[18].title;
        recipeItemTen.imageUrlOne = jsonObject.recipes[18].image_url;
        recipeItemTen.sourceUrlOne = jsonObject.recipes[18].source_url;
        ratingCalculator(recipeItemTen,parseInt(jsonObject.recipes[18].social_rank),1);
       // recipeItemTen.socialRankOne = parseInt(jsonObject.recipes[18].social_rank);
        recipeItemTen.titleTwo = jsonObject.recipes[19].title;
        recipeItemTen.imageUrlTwo = jsonObject.recipes[19].image_url;
        recipeItemTen.sourceUrlTwo = jsonObject.recipes[19].source_url;
        ratingCalculator(recipeItemTen,parseInt(jsonObject.recipes[19].social_rank),2);
       // recipeItemTen.socialRankTwo = parseInt(jsonObject.recipes[19].social_rank);
        recipeItemEleven.visible = true;
        recipeItemEleven.titleOne = jsonObject.recipes[20].title;
        recipeItemEleven.imageUrlOne = jsonObject.recipes[20].image_url;
        recipeItemEleven.sourceUrlOne = jsonObject.recipes[20].source_url;
        ratingCalculator(recipeItemEleven,parseInt(jsonObject.recipes[20].social_rank),1);
       // recipeItemEleven.socialRankOne = parseInt(jsonObject.recipes[20].social_rank);
        recipeItemEleven.titleTwo = jsonObject.recipes[21].title;
        recipeItemEleven.imageUrlTwo = jsonObject.recipes[21].image_url;
        recipeItemEleven.sourceUrlTwo = jsonObject.recipes[21].source_url;
        ratingCalculator(recipeItemEleven,parseInt(jsonObject.recipes[21].social_rank),2);
       // recipeItemEleven.socialRankTwo = parseInt(jsonObject.recipes[21].social_rank);
        recipeItemEleven.visible = true;
        recipeItemTwelve.titleOne = jsonObject.recipes[22].title;
        recipeItemTwelve.imageUrlOne = jsonObject.recipes[22].image_url;
        recipeItemTwelve.sourceUrlOne = jsonObject.recipes[22].source_url;
        ratingCalculator(recipeItemTwelve,parseInt(jsonObject.recipes[22].social_rank),1);
       // recipeItemTwelve.socialRankOne = parseInt(jsonObject.recipes[22].social_rank);
        recipeItemTwelve.titleTwo = jsonObject.recipes[23].title;
        recipeItemTwelve.imageUrlTwo = jsonObject.recipes[23].image_url;
        recipeItemTwelve.sourceUrlTwo = jsonObject.recipes[23].source_url;
        ratingCalculator(recipeItemTwelve,parseInt(jsonObject.recipes[23].social_rank),2);
       // recipeItemTwelve.socialRankTwo = parseInt(jsonObject.recipes[23].social_rank);
        recipeItemTwelve.visible = true;
        recipeItemThirteen.titleOne = jsonObject.recipes[24].title;
        recipeItemThirteen.imageUrlOne = jsonObject.recipes[24].image_url;
        recipeItemThirteen.sourceUrlOne = jsonObject.recipes[24].source_url;
        ratingCalculator(recipeItemThirteen,parseInt(jsonObject.recipes[24].social_rank),1);
       // recipeItemthirteen.socialRankOne = parseInt(jsonObject.recipes[24].social_rank);
        recipeItemThirteen.titleTwo = jsonObject.recipes[25].title;
        recipeItemThirteen.imageUrlTwo = jsonObject.recipes[25].image_url;
        recipeItemThirteen.sourceUrlTwo = jsonObject.recipes[25].source_url;
        ratingCalculator(recipeItemThirteen,parseInt(jsonObject.recipes[25].social_rank),2);
       // recipeItemthirteen.socialRankTwo = parseInt(jsonObject.recipes[25].social_rank);
        recipeItemThirteen.visible = true;
        recipeItemFourteen.titleOne = jsonObject.recipes[26].title;
        recipeItemFourteen.imageUrlOne = jsonObject.recipes[26].image_url;
        recipeItemFourteen.sourceUrlOne = jsonObject.recipes[26].source_url;
        ratingCalculator(recipeItemFourteen,parseInt(jsonObject.recipes[26].social_rank),1);
       // recipeItemFourteen.socialRankOne = parseInt(jsonObject.recipes[26].social_rank);
        recipeItemFourteen.titleTwo = jsonObject.recipes[27].title;
        recipeItemFourteen.imageUrlTwo = jsonObject.recipes[27].image_url;
        recipeItemFourteen.sourceUrlTwo = jsonObject.recipes[27].source_url;
        ratingCalculator(recipeItemFourteen,parseInt(jsonObject.recipes[27].social_rank),2);
       // recipeItemFourteen.socialRankTwo = parseInt(jsonObject.recipes[27].social_rank);
        recipeItemFourteen.visible = true;
        recipeItemFifteen.titleOne = jsonObject.recipes[28].title;
        recipeItemFifteen.imageUrlOne = jsonObject.recipes[28].image_url;
        recipeItemFifteen.sourceUrlOne = jsonObject.recipes[28].source_url;
        ratingCalculator(recipeItemFifteen,parseInt(jsonObject.recipes[28].social_rank),1);
       // recipeItemFifteen.socialRankOne = parseInt(jsonObject.recipes[28].social_rank);
        recipeItemFifteen.titleTwo = jsonObject.recipes[29].title;
        recipeItemFifteen.imageUrlTwo = jsonObject.recipes[29].image_url;
        recipeItemFifteen.sourceUrlTwo = jsonObject.recipes[29].source_url;
        ratingCalculator(recipeItemFifteen,parseInt(jsonObject.recipes[29].social_rank),2);
       // recipeItemFifteen.socialRankTwo = parseInt(jsonObject.recipes[29].social_rank);
        recipeItemFifteen.visible = true;
        recipeFlickable.visible = true;
    }

    function ratingCalculator(recipeObject,rate,object){
        var rating = parseInt((rate*5)/100);
        if(rating == 5){
            if(object == 1){
                recipeObject.socialRankOne = "images/fiveStar.png";
            }else{
                recipeObject.socialRankTwo = "images/fiveStar.png";
            }
        }else if(rating == 4){
            if(object == 1){
                recipeObject.socialRankOne = "images/fourStar.png";
            }else{
                recipeObject.socialRankTwo = "images/fourStar.png";
            }
        }else if(rating ==3){
            if(object == 1){
                recipeObject.socialRankOne = "images/threeStar.png";
            }else{
                recipeObject.socialRankTwo = "images/threeStar.png";
            }
        }else if(rating ==2){
            if(object == 1){
                recipeObject.socialRankOne = "images/twoStar.png";
            }else{
                recipeObject.socialRankTwo = "images/twoStar.png";
            }
        }else{
            if(object == 1){
                recipeObject.socialRankOne = "images/oneStar.png";
            }else{
                recipeObject.socialRankTwo = "images/oneStar.png";
            }
        }
    }

    Column {
        id : recipeResults
        anchors.top: parent.top
        anchors.topMargin: 10
        width: parent.width
        spacing: 10
        Row {
            anchors.horizontalCenter: parent.horizontalCenter
            spacing: 10

            AppButton {
                id: searchButton
                text : "Search for a dish"
                selected: recipeParent.selected
                unselected: recipeParent.unselected
                visible: true
                onClicked: {

                    startSpeechRecognitionReq("recipe", 2000);
                }
            }
        }
        Flickable {
            id:recipeFlickable
            width: recipeParent.width - 4
            height: recipeParent.height - 65
            contentWidth: recipeParent.width - 4
            contentHeight: allRecipes.height + 10
            clip: true
            visible : false
            Column {
                id: allRecipes
                anchors.top: parent.top
                anchors.topMargin: 4
                anchors.horizontalCenter: parent.horizontalCenter
                spacing: 35
                Recipe{
                    id : recipeItemOne
                    visible:false
                }
                Recipe{
                    id : recipeItemTwo
                    visible:false
                }
                Recipe{
                    id : recipeItemThree
                    visible:false
                }
                Recipe{
                    id : recipeItemFour
                    visible:false
                }
                Recipe{
                    id : recipeItemFive
                    visible:false
                }
                Recipe{
                    id : recipeItemSix
                    visible:false
                }
                Recipe{
                    id : recipeItemSeven
                    visible:false
                }
                Recipe{
                    id : recipeItemEight
                    visible:false
                }
                Recipe{
                    id : recipeItemNine
                    visible:false
                }
                Recipe{
                    id : recipeItemTen
                    visible:false
                }
                Recipe{
                    id : recipeItemEleven
                    visible:false
                }
                Recipe{
                    id : recipeItemTwelve
                    visible:false
                }
                Recipe{
                    id : recipeItemThirteen
                    visible:false
                }
                Recipe{
                    id : recipeItemFourteen
                    visible:false
                }
                Recipe{
                    id : recipeItemFifteen
                    visible:false
                }
            }
        }
        CustomWebView{
            id : recipeWebView
            url : ""
            width: parent.width
            height : 725
            visible : false
        }

    }

    AppButton {
        id: backButton
        text : "back"
        selected: recipeParent.selected
        unselected: recipeParent.unselected
        anchors.top:  parent.top
        anchors.topMargin: 10
        anchors.right: parent.right
        anchors.rightMargin: 30
        visible: false
        onClicked: {
             recipeFlickable.visible = true;
             recipeWebView.visible = false;
             backButton.visible = false;
             recipeWebView.url = "";
        }
    }

        Item {
            id: recipeSelector
            anchors.top: recipeParent.top
            anchors.topMargin: 50
            anchors.left: recipeParent.left
            anchors.leftMargin: 200
            width: content.width - 10
            height: content.height - 10

            Grid {
                anchors.left: parent.left
                anchors.leftMargin: 50
                anchors.top: parent.top
                anchors.topMargin: 60
                width: parent.width - 76
                height: parent.height - 120
                columns: 4
                rows: 3
                spacing: 145
                Button {
                    img: "images/appetizers.png"
                    text: "Appetizers"
                    onClicked: {
                        recipeSelector.visible = false;
                        var doc = new XMLHttpRequest();
                        dish = "appetizer";
                        doc.onreadystatechange = function() {
                           if (doc.readyState == XMLHttpRequest.DONE) {
                               var jsonObject = eval('(' + doc.responseText + ')');
                               loadJSON(jsonObject,parseInt(1));
                            }
                        }
                       doc.open("GET", "http://food2fork.com/F2F/api/search?key=c4c777e50d6e067663bd76e28326fbbc&q="+dish);
                       doc.send();

                    }
                }
                Button {
                    img: "images/salad.png"
                    text: "Salad"
                    onClicked: {

                        recipeSelector.visible = false;
                        var doc = new XMLHttpRequest();
                        dish = "salad";
                        doc.onreadystatechange = function() {
                           if (doc.readyState == XMLHttpRequest.DONE) {
                               var jsonObject = eval('(' + doc.responseText + ')');
                               loadJSON(jsonObject,parseInt(1));
                            }
                        }
                       doc.open("GET", "http://food2fork.com/F2F/api/search?key=c4c777e50d6e067663bd76e28326fbbc&q="+dish);
                       doc.send();
                    }
                }
                Button {
                    img: "images/sandwich.png"
                    text: "Sandwich"
                    onClicked: {
                        recipeSelector.visible = false;
                        var doc = new XMLHttpRequest();
                        dish = "sandwich";
                        doc.onreadystatechange = function() {
                           if (doc.readyState == XMLHttpRequest.DONE) {
                               var jsonObject = eval('(' + doc.responseText + ')');
                               loadJSON(jsonObject,parseInt(1));
                            }
                        }
                       doc.open("GET", "http://food2fork.com/F2F/api/search?key=c4c777e50d6e067663bd76e28326fbbc&q="+dish);
                       doc.send();
                    }
                }
                Button {
                    img: "images/snacks.png"
                    text: "Snacks"
                    onClicked: {
                        recipeSelector.visible = false;
                        var doc = new XMLHttpRequest();
                        dish = "snacks";
                        doc.onreadystatechange = function() {
                           if (doc.readyState == XMLHttpRequest.DONE) {
                               var jsonObject = eval('(' + doc.responseText + ')');
                               loadJSON(jsonObject,parseInt(1));
                            }
                        }
                       doc.open("GET", "http://food2fork.com/F2F/api/search?key=c4c777e50d6e067663bd76e28326fbbc&q="+dish);
                       doc.send();
                    }
                }

                Button {
                    img: "images/chicken.png"
                    text: "Chicken"
                    onClicked: {
                        recipeSelector.visible = false;
                        var doc = new XMLHttpRequest();
                        dish = "chicken";
                        doc.onreadystatechange = function() {
                           if (doc.readyState == XMLHttpRequest.DONE) {
                               var jsonObject = eval('(' + doc.responseText + ')');
                               loadJSON(jsonObject,parseInt(1));
                            }
                        }
                       doc.open("GET", "http://food2fork.com/F2F/api/search?key=c4c777e50d6e067663bd76e28326fbbc&q="+dish);
                       doc.send();
                    }
                }
                Button {
                    img: "images/beef.png"
                    text: "Beef"
                    onClicked: {
                        recipeSelector.visible = false;
                        var doc = new XMLHttpRequest();
                        dish = "beef";
                        doc.onreadystatechange = function() {
                           if (doc.readyState == XMLHttpRequest.DONE) {
                               var jsonObject = eval('(' + doc.responseText + ')');
                               loadJSON(jsonObject,parseInt(1));
                            }
                        }
                       doc.open("GET", "http://food2fork.com/F2F/api/search?key=c4c777e50d6e067663bd76e28326fbbc&q="+dish);
                       doc.send();
                    }
                }
                Button {
                    img: "images/pork.png"
                    text: "Pork"
                    onClicked: {
                        recipeSelector.visible = false;
                        var doc = new XMLHttpRequest();
                        dish = "pork";
                        doc.onreadystatechange = function() {
                           if (doc.readyState == XMLHttpRequest.DONE) {
                               var jsonObject = eval('(' + doc.responseText + ')');
                               loadJSON(jsonObject,parseInt(1));
                            }
                        }
                       doc.open("GET", "http://food2fork.com/F2F/api/search?key=c4c777e50d6e067663bd76e28326fbbc&q="+dish);
                       doc.send();
                    }
                }
                Button {
                    img: "images/seaFood.png"
                    text: "Sea Food"
                    onClicked: {
                        recipeSelector.visible = false;
                        var doc = new XMLHttpRequest();
                        dish = "sea food";
                        doc.onreadystatechange = function() {
                           if (doc.readyState == XMLHttpRequest.DONE) {
                               var jsonObject = eval('(' + doc.responseText + ')');
                               loadJSON(jsonObject,parseInt(1));
                            }
                        }
                       doc.open("GET", "http://food2fork.com/F2F/api/search?key=c4c777e50d6e067663bd76e28326fbbc&q="+dish);
                       doc.send();
                    }
                }
            }
        }

}




